package cn.iocoder.yudao.module.pypm.service.projectstage;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pypm.controller.admin.projectstage.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.projectstage.ProjectStageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pypm.dal.mysql.projectstage.ProjectStageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pypm.enums.ErrorCodeConstants.*;

/**
 * 项目阶段 Service 实现类
 *
 * @author 钦州码头
 */
@Service
@Validated
public class ProjectStageServiceImpl implements ProjectStageService {

    @Resource
    private ProjectStageMapper projectStageMapper;

    @Override
    public Long createProjectStage(ProjectStageSaveReqVO createReqVO) {
        // 插入
        ProjectStageDO projectStage = BeanUtils.toBean(createReqVO, ProjectStageDO.class);
        projectStageMapper.insert(projectStage);
        // 返回
        return projectStage.getId();
    }

    @Override
    public void updateProjectStage(ProjectStageSaveReqVO updateReqVO) {
        // 校验存在
        validateProjectStageExists(updateReqVO.getId());
        // 更新
        ProjectStageDO updateObj = BeanUtils.toBean(updateReqVO, ProjectStageDO.class);
        projectStageMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectStage(Long id) {
        // 校验存在
        validateProjectStageExists(id);
        // 删除
        projectStageMapper.deleteById(id);
    }

    private void validateProjectStageExists(Long id) {
        if (projectStageMapper.selectById(id) == null) {
            throw exception(PROJECT_STAGE_NOT_EXISTS);
        }
    }

    @Override
    public ProjectStageDO getProjectStage(Long id) {
        return projectStageMapper.selectById(id);
    }

    @Override
    public PageResult<ProjectStageDO> getProjectStagePage(ProjectStagePageReqVO pageReqVO) {
        return projectStageMapper.selectPage(pageReqVO);
    }

}